package com.glodon.paas.account.api.bean;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
@XmlRootElement(name = "user")
public class User implements Serializable{ 

	private static final long serialVersionUID = -202370214438785211L;

	private String id;

	//private String username;	//TODO 去掉
	
	private String loginname;

	private String email;

	private String mobile;

	private String realname;
	
	private Integer mainAccountType;
	
	private String nickname;	
	
	// 冗余字段，后台按默认逻辑给出显示名称
	private String displayName;

	// 冗余字段，后台按默认逻辑给出账号名
	private String accountName;

	private String gender;
	
	private Date birthday;
	
	private Integer birthdayType;

	private Address address;

	private Contact contact;

	private Company company;
	
	private Date createdTime;
	
	private Date updatedTime;;
	
	private boolean inActive;
	
	private List<Project> projects;
	
	//key:resource, value:privileges
	private Map<String, Set<String>> privileges;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

//	public String getUsername() {
//		return realname;
//	}
//
//	public void setUsername(String username) {
//		this.realname = username;
//	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Date getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}

	public boolean isInActive() {
		return inActive;
	}

	public void setInActive(boolean inActive) {
		this.inActive = inActive;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public Integer getBirthdayType() {
		return birthdayType;
	}

	public void setBirthdayType(Integer birthdayType) {
		this.birthdayType = birthdayType;
	}
	
	// 返回显示的名称
	public String getDisplayName() {
		if(StringUtils.isNotBlank(displayName)){
			return displayName;
		}
		
		if(StringUtils.isNotBlank(nickname)){
			return nickname;
		}

		if(StringUtils.isNotBlank(realname)){
			return realname;
		}
		
		if(StringUtils.isNotBlank(email)){
			return email;
		}
		
		if(StringUtils.isNotBlank(mobile)){
			return mobile;
		}
		
		return "";
	}
	
	public void setDisplayName(String displayName){
		this.displayName = displayName;
	}
	
	public Date getBirthday() {
		return this.birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	public void setYearMonthDay(String time) {
		this.birthday = new Date(Long.valueOf(time));
	}

	public Date getUpdatedTime() {
		return updatedTime;
	}

	public void setUpdatedTime(Date updatedTime) {
		this.updatedTime = updatedTime;
	}

	public String year() {
		if(birthday != null){
			Calendar c = Calendar.getInstance();
			c.setTime(birthday);
			return String.valueOf(c.get(Calendar.YEAR));
		}
		return "";
	}

	public String month() {
		if(birthday != null){
			Calendar c = Calendar.getInstance();
			c.setTime(birthday);
			return String.valueOf(c.get(Calendar.MONTH)+1);
		}
		return "";
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public Integer getMainAccountType() {
		return mainAccountType;
	}

	public void setMainAccountType(Integer mainAccountType) {
		this.mainAccountType = mainAccountType;
	}

	public void setProjects(List<Project> projects) {
        this.projects = projects;
    }
	
	public List<Project> getProjects() {
        return projects;
    }

    public String day() {
		if(birthday != null){
			Calendar c = Calendar.getInstance();
			c.setTime(birthday);
			return String.valueOf(c.get(Calendar.DAY_OF_MONTH));
		}
		return "";
	}
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

	public Map<String, Set<String>> getPrivileges() {
		return privileges;
	}

	public void setPrivileges(Map<String, Set<String>> privileges) {
		this.privileges = privileges;
	}
}
